const ExcelJs = require('exceljs');
const fs = require('fs');
const { waitTime } = require('../common');

async function updateExcel() {
    const workbook = new ExcelJs.Workbook()
    await workbook.xlsx.readFile('../file/in/WB CP重量差异大截图.xlsx');
    const worksheet1 = workbook.getWorksheet(1);
    const worksheet2 = workbook.getWorksheet(2);
    let trackingNumber = '';
    let index = 2;
    let row = 1;
    while (index > 1) {
        trackingNumber = worksheet1.getRow(index).getCell(2).value;
        if (!trackingNumber || trackingNumber.length === 0) {
            index = -1;
            break;
        }
        const numColis = trackingNumber.slice(0, 12);
        const buffer = fs.readFileSync(`../file/out/${numColis}.png`);
        const imageId = workbook.addImage({
            buffer,
            extension: 'png'
        });
        worksheet2.addImage(imageId, {
            tl: { col: 1, row: row },
            ext: { width: 750, height: 600 }
        });
        console.log(`index:${index} ===> ${trackingNumber} done`);
        index++;
        row+=36;
    }
    await workbook.xlsx.writeFile('../file/in/CP重量差异截图.xlsx');
}

updateExcel().then(() => {
    console.log('success');
})
